<template>
  <div style="z-index: 9999">
    <el-button size="small" type="primary" plain @click="importHandle">{{ $t('保存') }}</el-button>
    <div id="luckysheet" v-loading="loading" style="margin:0;padding:0;position:absolute;width:100%;top: 50px;bottom:0;" />
  </div>
</template>

<script>

import { getJmfReport } from '@/api/dev/jmf/jmfReport'
import { batchDealImport } from '@/api/base/deal/dealBase'
export default {
  data() {
    return {
      loading: false
    }
  },
  mounted() {
    this.initField()
  },
  methods: {
    // 初始化字段
    initField() {
      this.$nextTick(() => {
        var language = window.localStorage.getItem('language')
        switch (language) {
          case 'zh_cn':
            language = 'zh'
            break
          case 'en_us':
            language = 'en'
            break
        }
        window.luckysheet.destroy()
        var reportId = ''
        switch (this.$route.query.dealType) {
          case 'equity':
            reportId = '25eac91d3b824a48a32e08d6f8f46202'
                break
          case 'debt':
            reportId = 'ec6685a40fe441d7a90de6d8d46c2239'
                break
          case 'bf':
            reportId = 'fa8c528c0fe0497f9761db8b46bac605'
                break
        }
        getJmfReport(reportId).then(res => {
          const reportData = JSON.parse(res.data.reportData)
          reportData[0].scrollLeft = 0
          const options = {
            container: 'luckysheet', // 设定DOM容器的id
            title: 'Luckysheet Demo', // 设定表格名称
            lang: language, // 设定表格语言
            data: reportData,
            plugins: ['chart'],
            showinfobar: false // 是否显示顶部信息栏
          }
          // eslint-disable-next-line no-undef
          window.luckysheet.create(options)
        })
      })
    },
    importHandle() {
      this.loading = true
      const data = {
        excelData: window.luckysheet.getLuckysheetfile()[0].data,
        dealType: this.$route.query.dealType
      }
      batchDealImport(data).then(res => {
        if (res.msg === 'success') {
          setTimeout(() => {
            this.msgSuccess(this.$t('保存成功'))
            window.close()
          }, 1000)
        } else if (res.code === 500) {
          this.msgError(res.msg)
        } else {
          this.$alert(res.msg, this.$t('导入结果'), { dangerouslyUseHTMLString: true })
        }
        this.loading = false
      })
    }
  }
}
</script>

<style scoped>

</style>
